/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package view.HTMLUIcomponents;

import java.util.ArrayList;

import model.Labels;
import model.Vocabulary;

import org.apache.commons.lang.StringEscapeUtils;
import org.openrdf.model.URI;

import controller.HelperFunctions;
import controller.elementMapper.Mapper;


public class Synonyms extends AbstractComponent {
	

	public static final String CSSIDFIELD = "divsysnonyms";
	public static final String CSSIDCONTENT = "divsysnonymscontent";
	public static final String NAMEFORMELEMENT = "synonymsEntry";
	

	
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInViewMode(  URI uri, boolean showNavigation, boolean showNavigationInEditMode ) throws Exception {
		String ret = "";

		ret += getTitleContainer( model.Labels.SYNONYMS, null, false, true );
		ret += getFormContainer( getFormInViewMode( uri, showNavigation, showNavigationInEditMode ), CSSIDCONTENT );

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD );
		
		return ret;
	}
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInEditMode(  URI uri, String entry, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
		String ret = "";
		
		ret += getContentInEditMode(uri, entry, exceptions, showNavigation);

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD );
		
		return ret;
	}

	public static String getContentInEditMode(  URI uri, String entry, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
	
		String ret = "";
		
		ret += getTitleContainer( model.Labels.SYNONYMS, null, false, true );
		ret += getFormContainer( getFormInEditMode( uri, entry, exceptions, showNavigation ), CSSIDCONTENT );

		return ret;
	}
	
	public static String getFormInViewMode( URI uri, boolean showNavigation, boolean showNavigationInEditMode ) throws Exception {

		String ret = "";

		ArrayList<String> list = Mapper.selectSynonyms( uri );
		String content = getSynonymHTMLList(list);

		ret = renderViewForm( 
				content, 
				Mapper.selectHasVersionDateTimeString(uri),
				CSSIDCONTENT, 
				showNavigation, 
				showNavigationInEditMode );
		
		return ret;
	}
	
	private static String getSynonymHTMLList( java.util.ArrayList<String> entry ) {
		
		String ret = "";
		
		if( entry==null ) {
			
			ret += NOTHINGSELECTED;
		}
		else {
			
			ret += HelperFunctions.concatStrings( entry, HelperFunctions.SYNONYMSPLITTER );
			ret = StringEscapeUtils.escapeHtml( ret );
		}
		
		return ret.trim();	
	}
	


	public static String getFormInEditMode( URI uri, String entry, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
		
		String ret = "";

		String actualVersionContent = NOTHINGSELECTED;
		String actualTimestamp = Mapper.selectHasVersionDateTimeString(uri);

		String label = "";		
		if( uri!=null ) {

			ArrayList<String> tmp = Mapper.selectSynonyms( uri );
			if( tmp!=null ) {
				actualVersionContent = getSynonymHTMLList( tmp );
			}
			
			label = Mapper.selectLabel( uri, Vocabulary.ENGLISH);
			if( label==null )
				label = "";
		}		

		if(entry==null)
			entry="";
		
		if( !entry.equals(""))
			entry += HelperFunctions.SYNONYMSPLITTER;
		
		// content += getFormat( "Insert here Synonyms seperated by blanks (e.g. hotes bargains newyork (not new york))" );
		

		String content = "";
		
		content += 
			"<input style=\"width: 100%\" tabindex=\"\" id=\"" + NAMEFORMELEMENT + "\" name=\"" + NAMEFORMELEMENT + "\" value=\"" + entry + "\" type=\"text\" onkeyup=\"validateValue(this,document.getElementById('" + NAMEFORMELEMENT + "_errors'))\" />";
		
		content += "<div class=\"inputhint\">Separate your inputs with a blank symbol (e.g.: 'table desk').</div>";

		content += getWordNetTool( label );
				

		ret = renderEditForm(
				Labels.SYNONYMS,  
				content, 
				exceptions, 
				actualVersionContent, 
				actualTimestamp,
				CSSIDCONTENT, 
				NAMEFORMELEMENT, 
				showNavigation );


		return ret;
	}
	
	private static String getWordNetTool( String val ) {

		String ret = "";
		
		if( val==null ) 
			val = "";
		
		ret += "<div style=\"margin-top: 5px\">";
		
		ret += "Show what WordNet suggests for ";
		
		ret += "<input style=\"width: 80px;\" tabindex=\"\" id=\"wordnetEntry\" value=\"" + val + "\" type=\"text\" " +
				" onkeypress=\"if((event.keyCode==13)){document.getElementById('wordnetButton').click();}\"" +				
				" onkeyup=\"" +
					"if( this.value.trim()!='' ) " +
						"document.getElementById('wordnetButton').disabled=false; " +
					"else " +
						"document.getElementById('wordnetButton').disabled=true;" +
					"\"" +
				//"onblur=\"getWordNetEntry(this.value, " + model.DHTMLConstants.WORDNETENTRY + ", '" + wnd + "'); \"" +
				"/>";
		
		ret += " <input type=\"button\" value=\"WordNet Search\" id=\"wordnetButton\" ";
		if( val.equals("") )
			ret += "disabled=\"true\" ";				
		ret += 
				"onclick=\"getWordNetEntry(document.getElementById('wordnetEntry').value, " +
				"'" + model.DHTMLConstants.WORDNETENTRY + "', " +
				"'wordnetResult');\" />";
		
		ret += "</div>";
		
		ret += "<div id=\"wordnetResult\"></div>\n";
		
		return ret;
	}

}
